{% extends "base.html" %}
{# Copyright The IETF Trust 2015, All Rights Reserved #}
{% load origin %}
{% load django_bootstrap5 %}
{% load static %}
{% load misc_filters person_filters %}
{% block pagehead %}
    <link rel="stylesheet" href="{% static "ietf/css/list.css" %}">
    {{ all_forms|merge_media:'css' }}
{% endblock %}
{% block title %}Manage {{ clist.long_name }}{% endblock %}
{% block content %}
    {% origin %}
    <h1>Manage {{ clist.long_name }}</h1>
    {% bootstrap_messages %}
    <p>
        The list currently tracks
        <a href="{{ clist.get_absolute_url }}">{{ total_count }} document{{ total_count|pluralize }}</a>.
    </p>
    <p>
        <a class="btn btn-primary" href="{{ clist.get_absolute_url }}">Back to list</a>
    </p>
    <h2>Individual documents</h2>
    {% if individually_added %}
        <p>
            The list tracks {{ individually_added|length }} individually added document{{ individually_added|length|pluralize }}:
        </p>
        <table class="table table-sm table-striped tablesorter">
            <thead>
                <tr>
                    <th scope="col" data-sort="doc">Document</th>
                    <th scope="col">Manage</th>
                </tr>
            </thead>
            <tbody>
                {% for d in individually_added %}
                    <tr>
                        <td>{{ d.name }}</td>
                        <td>
                            <form method="post" id="remove_document_{{ d.pk }}">
                                {% csrf_token %}
                                <input type="hidden" name="document" value="{{ d.pk }}">
                                <button type="submit" class="btn btn-danger btn-sm" name="action" value="remove_document">Remove</button>
                            </form>
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <p>
            The list does not track any individually added documents yet.
        </p>
    {% endif %}
    {% if clist.group %}
        <p>
            Add individual documents here:
        </p>
    {% else %}
        <p>
            Conveniently track individual documents in your personal list with the track icon
            <i class="bi bi-bookmark"></i>
            in
            <a href="{% url 'ietf.doc.views_search.search' %}">search results</a>.
        </p>
        <p>
            You can also add documents here:
        </p>
    {% endif %}
    <form class="form add-document" method="post" id="add_document">
        {% csrf_token %}
        {% bootstrap_field add_doc_form.documents show_label=False %}
        {% bootstrap_button button_type="submit" name="action" value="add_documents" content="Add documents" %}
    </form>
    <h2>Search rules</h2>
    <p>
        You can track documents with a search rule. When a document fulfills the search criteria, it will automatically show up in the list.
    </p>
    {% if rules %}
        <table class="table table-sm table-striped tablesorter">
            <thead>
                <tr>
                    <th scope="col" data-sort="rule">Rule</th>
                    <th scope="col" data-sort="value">Value</th>
                    <th scope="col" data-sort="num">Matching Documents</th>
                    <th scope="col">Manage</th>
                </tr>
            </thead>
            <tbody>
                {% for rule in rules %}
                    <tr id="r{{ rule.pk }}">
                        <td>{{ rule.get_rule_type_display }}</td>
                        <td>
                            {% if "group" in rule.rule_type or "area" in rule.rule_type %}
                                {{ rule.group.acronym }}
                            {% elif "state_" in rule.rule_type %}
                                {{ rule.state }}
                            {% elif "author" in rule.rule_type or rule.rule_type == "ad" or "shepherd" in rule.rule_type %}
                                {% person_link rule.person %}
                            {% elif "name_contains" in rule.rule_type %}
                                {{ rule.text }}
                            {% endif %}
                        </td>
                        <td>{{ rule.matching_documents_count }}</td>
                        <td>
                            <form method="post">
                                {% csrf_token %}
                                <input type="hidden" name="rule" value="{{ rule.pk }}">
                                <button type="submit" class="btn btn-danger btn-sm" name="action" value="remove_rule">Remove</button>
                            </form>
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <p>
            No rules defined.
        </p>
    {% endif %}
    <div>
        <a class="btn btn-primary"
           data-bs-toggle="collapse"
           data-bs-target="#add-new-rule">Add a new rule</a>
    </div>
    <div id="add-new-rule"
         {% if not rule_type_form.errors and not rule_form %}class="collapse"{% endif %}>
        <form method="post">
            {% csrf_token %}
            {% bootstrap_form rule_type_form layout="horizontal" %}
            <div class="form-content-placeholder">
                {% if rule_form %}
                    {% bootstrap_form rule_form layout="horizontal" %}
                {% endif %}
            </div>
            <button type="submit" class="btn btn-primary" name="action" value="add_rule">Add rule</button>
        </form>
        <div class="empty-forms d-none">
            {% for rule_type, f in empty_rule_forms.items %}
                <div class="rule-type {{ rule_type }}">{% bootstrap_form f layout="horizontal" %}</div>
            {% endfor %}
        </div>
    </div>
{% endblock %}
{% block js %}
    {{ all_forms|merge_media:'js' }}
    <script src="{% static "ietf/js/list.js" %}"></script>
    <script src="{% static 'ietf/js/manage-community-list.js' %}"></script>
{% endblock %}